package com.lg.java.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


class MyHandler implements InvocationHandler {
    public Object impl;
    public  MyHandler(Object impl) {
        this.impl = impl;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("before");

//        method.invoke(this.impl, args);

        HelloImpl11 impl = (HelloImpl11) this.impl;
        impl.run1("111");

        System.out.println("after");
        return null;
    }
}

public class Demo2 {

    public static void main(String[] args) {
        MyHandler myHandler = new MyHandler(new HelloImpl11());
        Hello11 hello11 = (Hello11) Proxy.newProxyInstance(
                Hello11.class.getClassLoader(), // 传入ClassLoader
                new Class[] { Hello11.class }, // 传入要实现的接口
                myHandler); // 传入处理调用方法的InvocationHandler
        hello11.run("running");
    }
}

interface Hello11 {
    void run(String name);
}

class HelloImpl11 implements Hello11{
    @Override
    public void run(String name) {
        System.out.println(name+" --> running");
    }

    public void run1(String name) {
        System.out.println(name+" --> running");
    }

}

